package com.edward.service;


import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.Combo;
import com.edward.model.Fund;


public class FundManager extends HibernateDaoSupport {

	public void addFund(Fund fund) {
		
		getHibernateTemplate().save(fund);
	}
	
	public void updateFund(Fund fund) {
		
		getHibernateTemplate().update(fund);
	}
	
	public void deleteFund(int id){
		
		getHibernateTemplate().delete(getFund(id));
	}
	
	public Fund getFund(int id){
		Fund d =(Fund)getHibernateTemplate().get(Fund.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<Fund> listFund() {
		return getHibernateTemplate().find("from Fund");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit) {
		List <Fund>list = getHibernateTemplate().find("from Fund f order by f.sort" );
		Fund f = new Fund();
		for(Fund fund : list){
			f.setRway(f.getRway() + fund.getRway());
			f.setRsum(f.getRsum() + fund.getRsum());
			f.setRclose( f.getRclose() + fund.getRclose());
			f.setDway(f.getDway() + fund.getDway());
			f.setDsum(f.getDsum() + fund.getDsum());
			f.setDclose( f.getDclose() + fund.getDclose());}
		list.add(f);
		int total = list.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <Fund>datas = list.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	//经手人下拉框的添加
	@SuppressWarnings("unchecked")	
	public void addCheck(String value ,String type){
		List <Combo>list = getHibernateTemplate().find("from Combo c where c.value = '" + value + "' and c.type = '" + type + "'" );
		if(list.size() == 0){
			Combo c = new Combo();
			c.setType(type);
			c.setValue(value);
			getHibernateTemplate().save(c);
		}
		
		
	}
	
	@SuppressWarnings("unchecked")
	public List<Combo> getComboList(String type) {
		return getHibernateTemplate().find("from Combo c where c.type = '" + type + "'");
	}
	


}



